﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using Assets.Script.Tools;

public class LoadScene_Async : MonoBehaviour
{
    //显示进度的文本
    [SerializeField]private TextMeshProUGUI progressText;
    
    [Tooltip("下个场景的名字")]
    public static string nextSceneName;

    private AsyncOperation async = null;

    private bool animationEnd = false;

    private void Start()
    {
        Timer.TimerStart(0.5f, () =>
        {
            StartCoroutine(LoadScene());
        });
        Timer.TimerStart(1f, () =>
        {
            animationEnd = true;
        });
    }

    IEnumerator LoadScene()
    {
        async = SceneManager.LoadSceneAsync(nextSceneName);
        async.allowSceneActivation = false;
        bool isDone=false;
        while (!isDone)
        {
            //progressValue = async.progress;
            if (async.progress >= 0.89&&animationEnd)
            {
                async.allowSceneActivation = true;
                isDone = true;
            }
            yield return null;
        }
    }

    public static void LoadScene(string nextSceneName)
    {
        LoadScene_Async.nextSceneName = nextSceneName;
        Instantiate(Resources.Load<GameObject>(ResourcePath.Canvas_Loading));
        //SceneManager.LoadScene("LoadingScene");
    }
}
